package com.znsd.stu.controllor;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.znsd.entitys.Cwj_ClassInfo;
import com.znsd.entitys.GradeInfo;
import com.znsd.entitys.Cwj_SpecialtyInfo;
import com.znsd.entitys.Users;
import com.znsd.stu.service.Cwj_ClassService;
import com.znsd.stu.service.Cwj_SpecialtyService;
import com.znsd.stu.service.Cwj_TimeTableService;
import com.znsd.stu.service.Cwj_UserService;
import com.znsd.stu.service.UserService;
import com.znsd.stu.service.impl.Cwj_ClassServiceImpl;

@Controller
public class Cwj_ClassControllor {
	@Autowired
	private Cwj_ClassService classservice;
	@Autowired
	private Cwj_UserService users;
	@Autowired
	private Cwj_SpecialtyService Specialty;
	@Autowired
	private Cwj_TimeTableService times;
	
	
	@RequestMapping(value = "Updateclass", method = RequestMethod.POST)
	public String Updateclass(Integer classId,String clazz_name,Integer teacher,Integer specialty,HttpServletRequest  request) {
		HttpSession session=request.getSession();
		Users user=(Users) session.getAttribute("user");
		Cwj_ClassInfo info=new Cwj_ClassInfo();
		info.setClazz_id(classId);
		info.setClazz_createtime(new Date());
		info.setClazz_name(clazz_name);
		info.setTeacherId(teacher);
		classservice.update(info, teacher, specialty);
		return "admin/classes";
	}
	
	@ResponseBody
	@RequestMapping("getTeacherByClass")
	public Map<String,Object> getTeacherByClass(Integer id,HttpServletRequest  request) {
		List<Cwj_ClassInfo> classInfo=classservice.getByTeacherId(id);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("size",classInfo.size());
		return map;
	}
	@RequestMapping(value = "addclass", method = RequestMethod.POST)
	public String addclass(String clazz_name,Integer teacher,Integer specialty,HttpServletRequest  request) {
		HttpSession session=request.getSession();
		Users user=(Users) session.getAttribute("user");

		/* info.getUsers().setUser_id(user.getUser_id()); */
		classservice.addclass(clazz_name, new Date(), teacher, user.getUser_id(), specialty);
		Cwj_ClassInfo classinfo=classservice.getOrderBy();
		
		times.addtime("00:00:00","00:00:00","00:00:00","00:00:00","00:00:00",classinfo.getClazz_id(),user.getUser_id());
		return "admin/classes";
	}
	@RequestMapping(value = "addclasshb", method = RequestMethod.POST)
	public String addclasshb(Integer class1,Integer class2,String clazz_name,Integer teacher,Integer specialty,HttpServletRequest  request) {
		HttpSession session=request.getSession();
		Users user=(Users) session.getAttribute("user");
		classservice.addclass(clazz_name, new Date(), teacher, user.getUser_id(), specialty);
		Cwj_ClassInfo classInfo=classservice.getOrderBy();
		users.updateUserClass(classInfo.getClazz_id(),class1);
		users.updateUserClass(classInfo.getClazz_id(),class2);
		classservice.delClass(class1);
		classservice.delClass(class2);
		times.addtime("00:00:00","00:00:00","00:00:00","00:00:00","00:00:00",classInfo.getClazz_id(),user.getUser_id());
		times.deletetime(class1);
		times.deletetime(class2);
		return "admin/classes";
	}
	@RequestMapping(value = "goClass", method = RequestMethod.GET)
	public String Clazzs(HttpServletRequest  request) {
		return "admin/classes";
	}
	@RequestMapping(value = "goStuClass", method = RequestMethod.GET)
	public String goStuClass(HttpServletRequest  request) {
		return "admin/stuclass";
	}
	@RequestMapping(value = "goClasshb", method = RequestMethod.GET)
	public String goClasshb() {
		return "admin/classehb";
	}
	
	@RequestMapping(value = "goUpdateStuByClass", method = RequestMethod.GET)
	public String goUpdateStuByClass(Integer classId,HttpServletRequest  request) {
		HttpSession session=request.getSession();
		session.setAttribute("classId",classId);
		return "admin/stuupdateclass";
	}
	@ResponseBody
	@RequestMapping("getClassSession")
	public Map<String,Object> getClassSession(HttpServletRequest  request) {
		HttpSession session=request.getSession();
		Cwj_ClassInfo classInfo=(Cwj_ClassInfo) session.getAttribute("classInfo");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("classInfo",classInfo);
		return map;
	}
	@RequestMapping(value = "classesave", method = RequestMethod.GET)
	public String classesave(Integer classId,HttpServletRequest  request) {
		HttpSession session=request.getSession();
		Cwj_ClassInfo classInfo=classservice.getById(classId);
		session.setAttribute("classInfo",classInfo);
		return "admin/classesave";
	}
	@RequestMapping(value = "goClassDit", method = RequestMethod.GET)
	public String goClassDit(HttpServletRequest  request) {
		return "admin/classedit";
	}
	@ResponseBody
	@RequestMapping("classList")
	public Map<String,Object> classList() {
		Map<String,Object> map=new HashMap<String,Object>();
		List<Cwj_ClassInfo> list=classservice.getlist();
		for (int i = 0; i < list.size(); i++) {
			List<Users> userInfo=users.getStuByClass(list.get(i).getClazz_id());
			list.get(i).setCount(userInfo.size());
		}
		int count=classservice.getcount();
		map.put("rows",list);
		map.put("total",count);
		return map;
	}
	
	@ResponseBody
	@RequestMapping("gettecher")
	public Map<String,Object> gettecher() {
		Map<String,Object> map=new HashMap<String,Object>();
		List<Users> list=users.gettecher();
		List<Cwj_SpecialtyInfo> list1=Specialty.getAllByState();
		map.put("teacher",list);
		map.put("list", list1);
		return map;
	}
	@ResponseBody
	@RequestMapping("gethb")
	public Map<String,Object> gethb() {
		Map<String,Object> map=new HashMap<String,Object>();
		List<Users> list=users.gettecher();
		List<Cwj_ClassInfo> classInfo=classservice.getlist();
		List<Cwj_SpecialtyInfo> list1=Specialty.getAll();
		map.put("teacher",list);
		map.put("list", list1);
		map.put("classInfo",classInfo);
		return map;
	}
	@ResponseBody
	@RequestMapping("getStuByClass")
	public Map<String,Object> getStuByClass(Integer id) {
		Map<String,Object> map=new HashMap<String,Object>();
		List<Users> userInfo=users.getStuByClass(id);
		map.put("size",userInfo.size());
		return map;
	}
	@ResponseBody
	@RequestMapping("delClass")
	public Map<String,Object> delClass(Integer id) {
		Map<String,Object> map=new HashMap<String,Object>();
		classservice.delClass(id);
		times.deletetime(id);
		return map;
	}
	
	
}
